##############################################################################
#
# IperfClient.pm
# ==============
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   Modeling a iperf running in client mode in Linux
#
# Assumptions:
#   1.) iperf is running on Linux OS
#
# Supported methods:
#   All Linux's methods
#   new($ip, $userName, $passWord ; $port || 23);
#
# Note:
#   Variable after ";" is optional variable and value after "||" is 
#   default value
#
##############################################################################

package IperfServer;
$VERSION = 1.00;

@ISA = qw(Linux); # The @ISA array supports multiple inheritance
		  # For single parent use "use base" key word.

use strict;
use AutoTest::Exceptions;

# Constructor: new
# Purpose:     Instantiate a IperfServer object
# Parameters:
#       1.) ip : IP address of the node
#       2.) userName : User name used for login
#       3.) passWord : Password used for login
#       4.) port : A port to use for telnet, default value is 23
#
# Returns:    An instance of Network::IperfServer class
#
sub new ($$$;$) {
	my $class = shift;
	my $self = { };
	if (defined($_[3])) {
		$self = Node->new($_[0], $_[1], $_[2], $_[3]);
	} else {
		$self = Node->new($_[0], $_[1], $_[2]);
	}
	bless $self, $class;
	return $self;
}

# Method:     start
# Purpose:    Start the iperf in client mode
# Assumption:
#	1.) Linux host's network interface is set with the proper MTU.
#	    One can do this by using the "su" and then "setMTU" methods
#	    from parent class which is Linux.pm
#	    
# Parameters:
#       1.) serverIP : The IP address of the target iperf server
#       2.) duration : The duration of test
#       3.) trafficSize : If transport is UDP, trafficSize is the bandwidth
#			  utilization.  If transport is TCP, trafficSize
#			  is the amount of data user wish to transmit
#	4.) transport :  Optional transport designation.  Use "-u" for UDP
#			 and "" for TCP.
#	5.) mtuSize :  Optional MTU size of TCP data packet
#
# Returns:  Void
sub start ($$$;$;$) {
        my ($self) = shift;
        my ($serverIP) = shift;
        my ($duration) = shift;
        my ($trafficSize) = shift;
	if (defined($_[1])) {
        	my ($transport) = shift;
        	my ($mtuSize) = shift;
        } else if (defined($_[0])) {
        	my ($transport) = shift;
        } else {
        	my ($transport) = "";
	}
	my ($cmdPrompt) = $self->cmdPrompt();
	my ($savedTimeout) = $self->expectTimeout();
	$self->expectTimeout($duration + 30);
	my ($cmdString) = "";

	if ($transport eq "-u") {
        	$self->{exp}->send("iperf -c $serverIP $transport -t $duration -b $trafficSize\r");
		$cmdString = "iperf -c $serverIP $transport -t $duration -b $trafficSize";
	} else {
        	$self->{exp}->send("iperf -c $serverIP -t $duration -n $trafficSize -M $mtuSize -w 32K \r");
		$cmdString = "iperf -c $serverIP -t $duration -n $trafficSize -M $mtuSize -w 32K";
	}

	$self->{exp}->expect($self->expectTimeout(), '-re', $cmdPrompt) or 
		AutoTest::Exception::Expect->throw("Failed to start iperf client");
	$self->expectTimeout($savedTimeout);

        print "\n========================================================";
        print "\n Started Iperf Client Successfully";
        print "\n $cmdString";
        print "\n========================================================\n";

}
